import 'package:flutter/material.dart';

/// 通用确认对话框
class ConfirmDialog extends StatelessWidget {
  /// 对话框标题
  final String title;

  /// 对话框内容
  final String content;

  /// 确认按钮文本
  final String confirmText;

  /// 取消按钮文本
  final String cancelText;

  /// 确认按钮颜色
  final Color? confirmColor;

  /// 确认回调函数
  final VoidCallback onConfirm;

  /// 取消回调函数
  final VoidCallback? onCancel;

  const ConfirmDialog({
    Key? key,
    required this.title,
    required this.content,
    this.confirmText = '确定',
    this.cancelText = '取消',
    this.confirmColor,
    required this.onConfirm,
    this.onCancel,
  }) : super(key: key);

  /// 显示确认对话框的静态方法
  static Future<bool?> show({
    required BuildContext context,
    required String title,
    required String content,
    String confirmText = '确定',
    String cancelText = '取消',
    Color? confirmColor,
    VoidCallback? onConfirm,
    VoidCallback? onCancel,
  }) {
    return showDialog<bool>(
      context: context,
      builder:
          (context) => ConfirmDialog(
            title: title,
            content: content,
            confirmText: confirmText,
            cancelText: cancelText,
            confirmColor: confirmColor,
            onConfirm: () {
              Navigator.of(context).pop(true);
              if (onConfirm != null) {
                onConfirm();
              }
            },
            onCancel: () {
              Navigator.of(context).pop(false);
              if (onCancel != null) {
                onCancel();
              }
            },
          ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(title),
      content: Text(content),
      actions: [
        TextButton(
          onPressed: onCancel ?? () => Navigator.of(context).pop(false),
          child: Text(cancelText),
        ),
        ElevatedButton(
          onPressed: onConfirm,
          style:
              confirmColor != null
                  ? ElevatedButton.styleFrom(
                    backgroundColor: confirmColor!.withOpacity(0.1),
                    foregroundColor: confirmColor,
                  )
                  : null,
          child: Text(confirmText),
        ),
      ],
    );
  }
}
